﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PerformanceTests
{
    internal class TestTimeCounter
    {
        private DateTime clsTestStartTime;

        private DateTime clsSessionStartTime;

        private bool clsTestIsStarted;
        private bool clsSessionIsStarted;

        public void StartTest()
        {
            if (clsTestIsStarted)
            {
                throw new Exception("Test allready started!");
            }
            clsTestStartTime = DateTime.Now;
            clsTestIsStarted = true;
        }

        public void StartSession()
        {
            // a new session cannot be started if the last one have not been stoped!
            if (clsSessionIsStarted)
            {
                throw new Exception("Session allready started!");
            }
            
            // start the test also if not started yet. All measurement sessions are inside a test session.
            if (!clsTestIsStarted)
            {
                StartTest();
            }

            clsSessionStartTime = DateTime.Now;
            clsSessionIsStarted = true;
        }

        public void  StopSession()
        {
            // A session can be stopped only if it is Started
            if (!clsSessionIsStarted)
            {
                throw new Exception("Session is allready stopped or id did not have been started!");
            }

            clsSessionIsStarted = false;
        }

        public TimeSpan LastSessionDuration
        {
            get
            {
                return (DateTime.Now - clsSessionStartTime); 
            }
        }

        public void StopTest()
        {
            // A test  can be stopped only if it is Started
            if (!clsTestIsStarted)
            {
                throw new Exception("Test is allready stopped or id did not have been started!");
            }
            if (clsSessionIsStarted)
            {
                StopSession();
            }
            clsTestIsStarted = false;
        }

        public TimeSpan LastTestDuration
        {
            get
            {
                return (DateTime.Now - clsTestStartTime);
            }
        }

    }
}
